﻿Public Class frmMTurno

    Private Sub frmMTurno_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mdiPrincipal.tsmiTurno.Enabled = True
    End Sub

    Private Sub frmTurno_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        refreshList()
    End Sub

    Public Sub refreshList()

        Dim dstRecord As New DataSet
        Dim strCondicion As String = ""

        dstRecord.Tables.Add("VistaTurno")

        mdlCRUD.loadRecord(dstRecord, "VistaTurno", "vistaturno", strCondicion, Nothing)
        Me.dgvTurno.DataSource = dstRecord.Tables("VistaTurno")

        Me.dgvTurno.Columns(0).HeaderText = "Codigo"
        Me.dgvTurno.Columns(1).HeaderText = "Fecha"
        Me.dgvTurno.Columns(2).HeaderText = "Hora"
        Me.dgvTurno.Columns(3).HeaderText = "Doc"
        Me.dgvTurno.Columns(4).HeaderText = "No Doc"
        Me.dgvTurno.Columns(5).HeaderText = "Nombre Completo"
        Me.dgvTurno.Columns(6).HeaderText = "Fonoaudiología"
        Me.dgvTurno.Columns(7).HeaderText = "Optometría"
        Me.dgvTurno.Columns(8).HeaderText = "Psicología"
        Me.dgvTurno.Columns(9).HeaderText = "Médico General"
        Me.dgvTurno.Columns(10).HeaderText = "Médico Certificador"

        Me.dgvTurno.Columns(0).Width = 60
        Me.dgvTurno.Columns(1).Width = 80
        Me.dgvTurno.Columns(2).Width = 80
        Me.dgvTurno.Columns(3).Width = 60
        Me.dgvTurno.Columns(4).Width = 100
        Me.dgvTurno.Columns(5).Width = 250
        Me.dgvTurno.Columns(6).Width = 100
        Me.dgvTurno.Columns(7).Width = 100
        Me.dgvTurno.Columns(8).Width = 100
        Me.dgvTurno.Columns(9).Width = 120
        Me.dgvTurno.Columns(10).Width = 140

        colorGrid()

    End Sub

    Public Sub colorGrid()

        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim dgc As DataGridViewCell

        While i < Me.dgvTurno.Rows.Count
            While j < Me.dgvTurno.Columns.Count

                dgc = Me.dgvTurno.Rows(i).Cells(j)

                If j = 6 Then 'Fonoaudiología

                    If dgc.Value.ToString = "APLAZADO" Then

                        dgc.Style.BackColor = Color.Blue
                    ElseIf dgc.Value.ToString = "REPROBADO" Then

                        dgc.Style.BackColor = Color.Red
                    ElseIf dgc.Value.ToString = "APROBADO" Then

                        dgc.Style.BackColor = Color.Green
                    ElseIf dgc.Value.ToString = "EN CONSULTA" Then

                        dgc.Style.BackColor = Color.Yellow
                    End If

                End If

                    If j = 7 Then 'Optometría

                    If dgc.Value.ToString = "APLAZADO" Then

                        dgc.Style.BackColor = Color.Blue
                    ElseIf dgc.Value.ToString = "REPROBADO" Then

                        dgc.Style.BackColor = Color.Red
                    ElseIf dgc.Value.ToString = "APROBADO" Then

                        dgc.Style.BackColor = Color.Green
                    ElseIf dgc.Value.ToString = "EN CONSULTA" Then

                        dgc.Style.BackColor = Color.Yellow
                    End If

                    End If

                    If j = 8 Then 'Psicología

                    If dgc.Value.ToString = "APLAZADO" Then

                        dgc.Style.BackColor = Color.Blue
                    ElseIf dgc.Value.ToString = "REPROBADO" Then

                        dgc.Style.BackColor = Color.Red
                    ElseIf dgc.Value.ToString = "APROBADO" Then

                        dgc.Style.BackColor = Color.Green
                    ElseIf dgc.Value.ToString = "EN CONSULTA" Then

                        dgc.Style.BackColor = Color.Yellow
                    End If

                    End If

                    If j = 9 Then 'Médico General

                    If dgc.Value.ToString = "APLAZADO" Then

                        dgc.Style.BackColor = Color.Blue
                    ElseIf dgc.Value.ToString = "REPROBADO" Then

                        dgc.Style.BackColor = Color.Red
                    ElseIf dgc.Value.ToString = "APROBADO" Then

                        dgc.Style.BackColor = Color.Green
                    ElseIf dgc.Value.ToString = "EN CONSULTA" Then

                        dgc.Style.BackColor = Color.Yellow
                    End If

                    End If

                    If j = 10 Then 'Médico Certificador

                    If dgc.Value.ToString = "APLAZADO" Then

                        dgc.Style.BackColor = Color.Blue
                    ElseIf dgc.Value.ToString = "REPROBADO" Then

                        dgc.Style.BackColor = Color.Red
                    ElseIf dgc.Value.ToString = "APROBADO" Then

                        dgc.Style.BackColor = Color.Green
                    ElseIf dgc.Value.ToString = "EN CONSULTA" Then

                        dgc.Style.BackColor = Color.Yellow
                    End If

                    End If

                    j = j + 1
            End While
            i = i + 1
        End While

    End Sub

    Private Sub tsbNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbNuevo.Click
        Me.tsbNuevo.Enabled = False
        Me.renderForm("Nuevo - Turno", 1)
    End Sub

    Private Sub renderForm(ByVal strTitle As String, ByVal intMode As Integer)
        Dim frmTurno As New frmTurno

        frmTurno.MdiParent = mdiPrincipal
        frmTurno.frmParent = Me

        Dim strCondicion As String = " "
        Dim especialidad_id As Integer
        especialidad_id = 2

        Select Case especialidad_id
            Case 1 'Fonoaudiologia
                strCondicion = " aspirante_fonoaudilogia = 0 AND aspirante_optometria <> 1 AND aspirante_psicologia <> 1 AND aspirante_medicogeneral <> 1 AND aspirante_medicocertificador <> 1"
            Case 2 'Optometria
                strCondicion = " aspirante_optometria = 0 AND aspirante_fonoaudilogia <> 1 AND aspirante_psicologia <> 1 AND aspirante_medicogeneral <> 1 AND aspirante_medicocertificador <> 1"
            Case 3 'Psicologia
                strCondicion = " aspirante_psicologia = 0 AND aspirante_fonoaudilogia <> 1 AND aspirante_optometria <> 1 AND aspirante_medicogeneral <> 1 AND aspirante_medicocertificador <> 1"
            Case 4 'Medicina General

                If mdlHerramientas.boolTurnero = True Then

                    strCondicion = " aspirante_medicogeneral = 0 AND aspirante_fonoaudilogia >= 1 AND aspirante_optometria >= 1 AND aspirante_psicologia >= 1 AND aspirante_medicocertificador = 0"
                Else

                    strCondicion = " aspirante_medicogeneral = 0 AND aspirante_fonoaudilogia <> 1 AND aspirante_optometria <> 1 AND aspirante_psicologia <> 1 AND aspirante_medicocertificador <> 1"
                End If

            Case 5 'Medico Certificador

                If mdlHerramientas.boolTurnero = True Then
                    strCondicion = " aspirante_medicocertificador = 0 AND aspirante_fonoaudilogia >= 1 AND aspirante_optometria >= 1 AND aspirante_psicologia >= 1 AND aspirante_medicogeneral >= 1"
                Else
                    strCondicion = " aspirante_medicocertificador = 0 AND aspirante_fonoaudilogia <> 1 AND aspirante_optometria <> 1 AND aspirante_psicologia <> 1 AND aspirante_medicogeneral <> 1"
                End If

        End Select

        Dim dstRecord As New DataSet
        dstRecord.Tables.Add("VistaEspecialidadTurno")
        mdlCRUD.loadRecord(dstRecord, "VistaEspecialidadTurno", "vistaespecialidadturno", strCondicion, "aspirante_id")

        frmTurno.cmbAspirante.DataSource = dstRecord.Tables("VistaEspecialidadTurno")
        frmTurno.cmbAspirante.DisplayMember = dstRecord.Tables("VistaEspecialidadTurno").Columns(1).Caption.ToString
        frmTurno.cmbAspirante.ValueMember = dstRecord.Tables("VistaEspecialidadTurno").Columns(0).Caption.ToString

        frmTurno.modeForm = intMode
        frmTurno.Text = strTitle
        frmTurno.Show()

    End Sub

    Private Sub tsbRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbRefresh.Click
        Me.refreshList()
    End Sub

    Private Sub dgvTurno_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvTurno.CellMouseClick
        colorGrid()
    End Sub

    Private Sub dgvTurno_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvTurno.ColumnHeaderMouseClick
        colorGrid()
    End Sub

    Private Sub frmMTurno_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        colorGrid()
    End Sub
End Class